//
//  TrainInfoCtrl.m
//  laowu
//
//  Created by Northwan on 15/4/29.
//  Copyright (c) 2015年 长沙市岳麓区立早网络科技有限公司. All rights reserved.
//

#import "TrainInfoCtrl.h"
#import "TrainInfoCell.h"
#import "TrainInfoHeaderView.h"
#import "TrainInfoDetailCtrl.h"
#import "HomeDataTool.h"

@interface TrainInfoCtrl ()<UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate>
@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)TrainInfoHeaderView *headerView;
@property(nonatomic,strong)NSMutableArray *trainInfoArray;
@end

@implementation TrainInfoCtrl

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"培训机构";
    self.trainInfoArray = [NSMutableArray array];
    [self initHeaderView];
    [self initTableView];
    [self.tableView headerBeginRefreshing];
}

-(void)getDataWithMaxId:(NSInteger)maxId keywords:(NSString*)keywords{
    [HomeDataTool getTrainInfoData:maxId keywords:keywords success:^(TrainInfoRes *result) {
        [Utils refreshComplete:self.tableView];
        if(result.isSucc){
            if (maxId==0) {
                [self.trainInfoArray removeAllObjects];
            }
            [self.trainInfoArray addObjectsFromArray:result.datas];
            [self.tableView reloadData];
        }else{
            [DialogUtils showTipsWithView:self.view String:result.msg];
        }
    } failure:^(NSError *error) {
        [Utils refreshComplete:self.tableView];
        [DialogUtils showTipsWithView:self.view String:@"加载失败"];
    }];
}

-(void)initHeaderView{
    _headerView =[[UINib nibWithNibName:@"TrainInfoHeaderView" bundle:[NSBundle mainBundle]] instantiateWithOwner:self options:nil][0];
    _headerView.searchView.delegate = self;
    [_headerView setFrame:CGRectMake(0, 0, SCREENW, 100)];
    [self.view addSubview:_headerView];
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    [self.view endEditing:YES];
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [self.view endEditing:YES];
//    if(ISBLANK(_headerView.searchView.text)){
//        [DialogUtils showTipsWithView:self.view String:@"搜索关键字为空"];
//    }else{
    
        [self getDataWithMaxId:0 keywords:_headerView.searchView.text];
        NSLog(@"搜索:%@",_headerView.searchView.text);
//    }
    return YES;
}

// 初始化tabView
-(void)initTableView{
    _tableView = [[UITableView alloc]init];
    _tableView.frame = CGRectMake(0, CGRectGetMaxY(_headerView.frame), SCREENW, SCREENH-64-CGRectGetMaxY(_headerView.frame));
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    [_tableView addHeaderWithTarget:self action:@selector(headerReferesh)];
    [_tableView addFooterWithTarget:self action:@selector(footerReferesh)];
    [_tableView setTableFooterView:[[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREENW, 0)]];
}

-(void)headerReferesh{
    [self getDataWithMaxId:0 keywords:_headerView.searchView.text];
}

-(void)footerReferesh{
    NSInteger maxId = 0;
    if (self.trainInfoArray.count) {
        WorkReportItemResult *item = [self.trainInfoArray lastObject];
        maxId = item.id;
    }
    if(maxId > 0){
       maxId =  maxId-1;
    }
    [self getDataWithMaxId:maxId keywords:_headerView.searchView.text];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.trainInfoArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    TrainInfoCell *cell = [TrainInfoCell cellWithTableView:tableView indexPath:indexPath];
    [cell initData:self.trainInfoArray[indexPath.row]];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.navigationController pushViewController:[TrainInfoDetailCtrl initWithTrainInfo:self.trainInfoArray[indexPath.row]] animated:YES];
    //处理cell点击效果
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return TRAIN_INFO_CELL_HEIGHT;
}

@end
